package com.travelsupport.controller;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.travelsupport.myobject.AdvertiseObj;
import com.travelsupport.myobject.CatalogObj;
import com.travelsupport.myobject.CurrencyObj;
import com.travelsupport.myobject.LocationObj;
import com.travelsupport.ui.R;

public class DbController {

	private static String TAG = com.travelsupport.common.Common.TAG;
	private static String DB_PATH = com.travelsupport.common.Common.DB_FILEPATH;
	private static String DB_NAME = com.travelsupport.common.Common.DB_NAME;

	private final Context context;
	public SQLiteDatabase db = null;
	public CatalogObj catalog = null;
	private static DbController mInstance = null;
	private final MySQLiteHelper sqliteHelper;

	public DbController(Context context) {
		sqliteHelper = new MySQLiteHelper(context);
		this.context = context;
	}

	public void openDb() {
		db = sqliteHelper.getWritableDatabase();
	}

	public void closeDb() {
		sqliteHelper.close();
	}

	public void readDb() {
		db = sqliteHelper.getReadableDatabase();
	}

	/***
	 * CATALOG QUERIES
	 */

	public List<CatalogObj> selectAllCatalog() {
		List<CatalogObj> lstCatalog = new ArrayList<CatalogObj>();
		Cursor cursor = db
				.query(sqliteHelper.TBL_CATALOG, sqliteHelper.ALL_CATALOG,
						sqliteHelper.CATALOG_SUBID + " IS NULL", null,
						sqliteHelper.CATALOG_ID, null, null);

		if (cursor != null) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				CatalogObj catalogObj = cursorToCatalog(cursor);
				cursor.moveToNext();
				lstCatalog.add(catalogObj);
			}
		}
		cursor.close();
		return lstCatalog;
	}

	public List<CatalogObj> selectAllSubCatalog(long catalogID) {
		List<CatalogObj> lstSubCatalog = new ArrayList<CatalogObj>();
		Cursor cursor = db.query(sqliteHelper.TBL_CATALOG,
				sqliteHelper.ALL_CATALOG, sqliteHelper.CATALOG_SUBID + " = "
						+ catalogID, null, sqliteHelper.CATALOG_ID, null, null);

		if (cursor != null) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				CatalogObj catalogObj = cursorToCatalog(cursor);
				cursor.moveToNext();
				lstSubCatalog.add(catalogObj);
			}
		}
		cursor.close();
		return lstSubCatalog;
	}

	public CatalogObj cursorToCatalog(Cursor cursor) {
		CatalogObj catalogObj = new CatalogObj();

		catalogObj.set_id(cursor.getLong(0));
		catalogObj.setName(cursor.getString(1));
		catalogObj.setIcon(cursor.getBlob(2));
		catalogObj.setCatalogID(cursor.getLong(3));

		return catalogObj;
	}

	/*
	 * END CATALOG QUERIES
	 */

	public void selectMyFavorite() {

	}

	public void updateLocation() {

	}

	public void updateLocations() {

	}

	/*
	 * public static DbController getInstance(Context ctx) { if(mInstance ==
	 * null){ mInstance = new DbController(ctx.getApplicationContext()); }
	 * 
	 * return mInstance; }
	 */

	/*
	 * LOCATION QUERIES
	 */

	// Select all locations
	public List<LocationObj> selectAllLocation() {
		List<LocationObj> loationList = new ArrayList<LocationObj>();
		loationList.clear();
		Cursor cursor = db.rawQuery("select * from "
				+ MySQLiteHelper.TBL_LOCATION, null);
		if (cursor.moveToFirst()) {
			do {
				LocationObj locationObj = cursorToLocation(cursor);
				cursor.moveToNext();
				loationList.add(locationObj);
			} while (cursor.moveToNext());
		}
		return loationList;
	}

	// Select a location
	public LocationObj selectLocation(int locationID) {
		LocationObj locationObj = new LocationObj();
		Cursor cursor = db.query(MySQLiteHelper.TBL_LOCATION,
				MySQLiteHelper.ALL_LOCATION, MySQLiteHelper.LOCATION_ID + " = "
						+ locationID, null, MySQLiteHelper.CATALOG_ID, null,
				null);
		if (cursor != null) {
			cursor.moveToFirst();
			locationObj = cursorToLocation(cursor);
		}
		return locationObj;
	}

	public LocationObj cursorToLocation(Cursor cursor) {
		LocationObj locationObj = new LocationObj();

		locationObj.set_id(cursor.getLong(0));
		locationObj.setName(cursor.getString(1));
		locationObj.setAddress(cursor.getString(2));
		locationObj.setPhone(cursor.getString(3));
		locationObj.setWebsite(cursor.getString(4));
		locationObj.setImage(cursor.getBlob(5));
		locationObj.setDescription(cursor.getString(6));
		locationObj.setPriceID(cursor.getLong(7));
		locationObj.setSubCatalogID(cursor.getLong(8));
		locationObj.setQuanlityID(cursor.getLong(9));
		locationObj.setLatitude(cursor.getFloat(10));
		locationObj.setLongtitude(cursor.getFloat(11));
		locationObj.setFavorite(cursor.getInt(12));

		return locationObj;
	}

	/*
	 * END LOCATION QUERIES
	 */

	public void selectPrices() {

	}

	public void selectQuanlities() {

	}

	public void filterLocation() {

	}

	public void insertMyFavorite() {

	}

	public boolean checkVersion() {
		return false;
	}
	//advertise
	public AdvertiseObj selectAdvertise() {
		AdvertiseObj adObj = new AdvertiseObj();
		Cursor c1 = getAll(sqliteHelper.TBL_ADVERTISE);
		if (c1 != null & c1.getCount() != 0) {
			c1.moveToFirst();
			adObj.setName(c1.getString(c1.getColumnIndex("adName")));
			adObj.setURL(c1.getString(c1.getColumnIndex("adURL")));
		}
		return adObj;
	}
	// Currency
	public Cursor getAll(String tableName) {
		return (db.rawQuery("SELECT * FROM " + tableName, null));
	}

	public List<CurrencyObj> selectAllCurrency() {
		List<CurrencyObj> currencyList = new ArrayList<CurrencyObj>();
		currencyList.clear();
		Cursor c1 = getAll(sqliteHelper.TBL_CURRENCY);
		Log.d("Phuong", "c1 number");
		if (c1 != null & c1.getCount() != 0) {

				c1.moveToFirst();
				CurrencyObj usdItems = new CurrencyObj();
				usdItems.set_item_currency(R.drawable.usd_flag);
				usdItems.setCurrency(c1.getFloat(c1.getColumnIndex("USD")));
				currencyList.add(usdItems);

				CurrencyObj uerItems = new CurrencyObj();
				uerItems.set_item_currency(R.drawable.eur_flag);
				uerItems.setCurrency(c1.getFloat(c1.getColumnIndex("EUR")));
				currencyList.add(uerItems);

				CurrencyObj yenItems = new CurrencyObj();
				yenItems.set_item_currency(R.drawable.yen_flag);
				yenItems.setCurrency(c1.getFloat(c1.getColumnIndex("YEN")));
				currencyList.add(yenItems);

				CurrencyObj rmbItems = new CurrencyObj();
				rmbItems.set_item_currency(R.drawable.rmb_flag);
				rmbItems.setCurrency(c1.getFloat(c1.getColumnIndex("RMB")));
				currencyList.add(rmbItems);
				// }while (c1.moveToNext());

		}
		return currencyList;
	}

}
